from typing import List

from pydantic import EmailStr
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from model.base import Base


class UserBase:
    name: Mapped[str] = mapped_column()
    id_role: Mapped[int] = mapped_column(ForeignKey("role.id"))
    login: Mapped[str] = mapped_column(unique=True)
    Id_level_worker: Mapped[int] = mapped_column()


class User(UserBase, Base):
    __tablename__ = 'user'

    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    password: Mapped[str] = mapped_column(nullable=False)
    role: Mapped["Role"] = relationship(back_populates="users")


class Role(Base):
    __tablename__ = 'role'

    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    name: Mapped[str] = mapped_column(nullable=False, unique=True)

    users: Mapped[List[User]] = relationship("User", back_populates="role")


class AddressList(Base):
    __tablename__ = 'address_list'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    path: Mapped[str] = mapped_column()
    id_type_address: Mapped[int] = mapped_column(ForeignKey("type_address.id"))
    type_address: Mapped["TypeAddress"] = relationship(back_populates="addresses")
    latitude: Mapped["str"] = mapped_column()
    longitude: Mapped["str"] = mapped_column()


class TypeAddress(Base):
    __tablename__ = 'type_address'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    name: Mapped[str] = mapped_column()
    addresses: Mapped[List[AddressList]] = relationship("AddressList", back_populates="type_address")


class RespawnCourier(Base):
    __tablename__ = 'respawn_courier'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    id_address_list: Mapped[int] = mapped_column(ForeignKey("address_list.id"))
    id_user: Mapped[int] = mapped_column(ForeignKey("user.id"))

    user: Mapped[User] = relationship()
    address: Mapped[AddressList] = relationship()


class SubTask(Base):
    __tablename__ = 'sub_task'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    date: Mapped[int]
    title: Mapped[str]


class Task(Base):
    __tablename__ = 'task'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    date_start_event: Mapped[int]
    title: Mapped[str]
    id_tolmut: Mapped[int]


class Schedule(Base):
    __tablename__ = 'schedule'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    id_task: Mapped[int] = mapped_column(ForeignKey("task.id"))
    id_user: Mapped[int] = mapped_column(ForeignKey("user.id"))
    id_sub_task: Mapped[int] = mapped_column(ForeignKey("sub_task.id"))
    id_tolmut: Mapped[int]
    status_task: Mapped[int]
    date: Mapped[int]
    id_address_list: Mapped[int] = mapped_column(ForeignKey("address_list.id"))

    address: Mapped[AddressList] = relationship()
    task: Mapped[Task] = relationship()
    sub_task: Mapped[SubTask] = relationship()


class Tolmut(Base):
    __tablename__ = 'tolmut'
    id: Mapped[int] = mapped_column(default=None, nullable=False, primary_key=True)
    name: Mapped[str] = mapped_column()
    time_is_out: Mapped[int] = mapped_column()
    id_continion: Mapped[int] = mapped_column()
    id_condition_two: Mapped[int] = mapped_column()

